package com.xu.controller;

import com.xu.pojo.User;
import com.xu.pojo.UserDto;
import com.xu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @RequestMapping("/user/toInfo")
    public String queryInfo(Model model){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        User user = userService.queryUserByID(userDto.getUserId());
        model.addAttribute("user",user);
        return "user/infor";
    }

    @RequestMapping("/user/toPwd")
    public String toPwd(){
        return "user/pwd";
    }

    @PostMapping("/user/updatePwd")
    public String updatePwd(String pwd,String pwdConfirm,Model model){
        if (!pwd.equals(pwdConfirm)) {
            model.addAttribute("msg","两次密码不一致");
            return "user/pwd";
        }
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        userService.updatePwd(userDto.getUserId(),pwd);
        model.addAttribute("msg","密码修改成功");
        return "user/pwd";
    }



}
